import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'dart:math' as Math;

import '../Gesture/GestureDetectorHitTest.dart';

class ClassRoomTopicPage extends StatefulWidget {
  @override
  _ClassRoomTopicPageState createState() {
    return _ClassRoomTopicPageState();
  }
}

class _ClassRoomTopicPageState extends State<ClassRoomTopicPage> with TickerProviderStateMixin {
  double height = 100;
  late Size screenSize;


  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    visitAncestorElements1(aa);
  }

  bool aa(String  a){
    print("tgw$a");
    return true;
  }

  void visitAncestorElements1(bool Function(String element) visi) {
    visi("safasf");
  }



  @override
  Widget build(BuildContext context) {
    screenSize = MediaQuery.of(context).size;

    return WillPopScope(
      child: AnnotatedRegion<SystemUiOverlayStyle>(
        value: SystemUiOverlayStyle.dark,
        child: Scaffold(
          backgroundColor: Colors.white,
          body: SafeArea(
            child: Stack(
              children: [
                Column(
                  children: _buildChildren(),
                ),
                _buildHasChildrenContent()

              ],
            ),
          ),
        ),
      ),
      onWillPop: () async {
        print("返回按钮");
        return false;
      },
    );
  }

  List<Widget> _buildChildren() {
    List<Widget> widgets = [
      Padding(
        padding: EdgeInsets.all(10),
        child: _buildChildrenTitle(),
      ),
      Container(
        height: 1,
        color: Color(0xFFE2DDDD),
      ),
      Padding(
        padding: EdgeInsets.all(10),
        child: _buildChildrenSelectTopic(),
      ),
      _buildChildrenContent(),
    ];
    return widgets;
  }
  List<Widget> tabs = [Tab(child: Text("title1")),Tab(child: Text("title2")),Tab(child: Text("title3"))];
  List<Widget> tabViews = [Container(child: Text("title1asf")),Container(child: Text("title1af")),Container(child: Text("titleafa1"))];




  Widget _buildHasChildrenContent() {
    var tabControl= TabController(
      length: 3,
      initialIndex: 2, vsync:  this ,
    );

    var content = HitTestBlocker(
      child:  Container(
        height: screenSize.height,
        color: Color(0xFFFFFF),
        child: Align(
          alignment: Alignment.bottomCenter,
          child: Container(
            height: height,
            color: Color(0xFF21D24D),
            child: GestureDetector(
              onVerticalDragUpdate: (details) {
                print("-----拖动:${details.delta.dy}");
                print("-----拖动:$height");
                if((height>=screenSize.height-100 && details.delta.dy<=0)||(height<=100 && details.delta.dy>=0)){
                  return;
                }
                height -= details.delta.dy;

                setState(() {});
              },
              onVerticalDragEnd: (details) {},
              child: Column(children: [
                TabBar(
                  isScrollable: true,
                  // 选中时的字体颜色
                  labelStyle: TextStyle(
                      color:Color(0xFFE21C1C),
                      fontSize: 24,
                      fontWeight: FontWeight.w500),
                  // 选中时的字体样式（颜色设置在这里不起作用，显示的总是白色）
                  unselectedLabelStyle: TextStyle(
                      color:Color(0xFF001EFF),
                      fontSize: 140,
                      fontWeight: FontWeight.normal),
                  unselectedLabelColor:   Color(0xffad1a98),
                  // 未选中时的字体样式
                  labelPadding: EdgeInsets.symmetric(horizontal: 32),
                  // 文本的左右间距

                  indicatorSize: TabBarIndicatorSize.label,
                  // 滚动条的左右间距
                  // 滚动条的样式
                  tabs: tabs,
                  controller: tabControl,
                ),
                Expanded(child: TabBarView(children: tabViews,controller: tabControl,physics: ClampingScrollPhysics(),)
                )
              ],),
            ),
          ),

        ),

      )
      ,
    );
    return content;
  }

  Widget _buildChildrenContent() {
    var content = Flexible(
      fit: FlexFit.loose,
      flex: 1,
      child: Padding(
        padding: EdgeInsets.all(10),
        child: RawScrollbar(
          shape: StarShape(),
          thumbColor: Color(0xFFC91919),
          child: ListView.builder(
            shrinkWrap: true, //范围内进行包裹（内容多高ListView就多高）
            itemCount: 20,
            itemExtent: 50.0,
            itemBuilder: (context, index) => Text("$index"),
          ),
        ),
      ),
    );
    return content;
  }

  Widget _buildChildrenTitle() {
    var title = Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Container(
          width: 120,
          child: Row(
            children: [
              GestureDetector(
                child: Image.asset("images/student_answer_icon_navi_back.png"),
                onTap: () {
                  Navigator.pop(context);
                },
              ),
              DecoratedBox(
                position: DecorationPosition.background,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(8.0), //3像素圆角,矩形独有
                    color: Color(0xFFF6EFEF)),
                child: Padding(
                    padding:
                        EdgeInsets.only(left: 8, right: 8, top: 4, bottom: 4),
                    child: Text("00:00:00")),
              ),
            ],
          ),
          color: Color(0xFFD71D1D),
        ),
        Expanded(
          child: Center(
            child: Text(
              "标题发生法发水水水水洒洒水反反复复烦烦烦",
              overflow: TextOverflow.ellipsis,
            ),
          ),
        ),
        Container(
          alignment: Alignment.centerRight,
          width: 120,
          child: UnconstrainedBox(
            child: DecoratedBox(
              position: DecorationPosition.background,
              decoration: BoxDecoration(
                color: Color(0xFF15A3EF),
                border: Border.all(
                  color: Colors.black.withOpacity(0.5),
                ),
                borderRadius: BorderRadius.circular(8.0), //3像素圆角,矩形独有
                // color: Color(0xFFF6EFEF)
              ),
              child: Padding(
                  padding:
                      EdgeInsets.only(left: 8, right: 8, top: 4, bottom: 4),
                  child: Text("答题卡")),
            ),
          ),
          color: Color(0xFFD71D1D),
        ),
      ],
    );
    return title;
  }

  Widget _buildChildrenSelectTopic() {
    var title = Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Container(
          child: Row(
            textBaseline: TextBaseline.alphabetic,
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              Text(
                "2",
                textAlign: TextAlign.end,
                style: TextStyle(fontSize: 18, height: 1),
              ),
              Text("/"),
              Text(
                "5",
                textAlign: TextAlign.end,
                style: TextStyle(fontSize: 14, height: 1.4),
              ),
              Text(
                "【单选/多选】",
                textAlign: TextAlign.end,
                style: TextStyle(fontSize: 14, height: 1.4),
              )
            ],
          ),
          color: Color(0xFFD71D1D),
        ),
        Container(
          child: Row(
            children: [
              GestureDetector(
                child: Image.asset("images/student_answer_icon_navi_back.png"),
                onTap: () {
                  Navigator.pop(context);
                },
              ),
              DecoratedBox(
                position: DecorationPosition.background,
                decoration: BoxDecoration(
                    borderRadius: BorderRadius.circular(8.0), //3像素圆角,矩形独有
                    color: Color(0xFFF6EFEF)),
                child: Padding(
                    padding:
                        EdgeInsets.only(left: 8, right: 8, top: 4, bottom: 4),
                    child: Text("00:00:00")),
              ),
            ],
          ),
          color: Color(0xFFD71D1D),
        ),
      ],
    );
    return title;
  }
}

class StarShape extends OutlinedBorder {
  ///
  /// The [side] argument must not be null.
  const StarShape({BorderSide side = BorderSide.none})
      : assert(side != null),
        super(side: side);

  @override
  EdgeInsetsGeometry get dimensions {
    return EdgeInsets.all(side.width);
  }

  @override
  ShapeBorder scale(double t) => StarShape(side: side.scale(t));

  @override
  ShapeBorder? lerpFrom(ShapeBorder? a, double t) {
    if (a is StarShape)
      return StarShape(side: BorderSide.lerp(a.side, side, t));
    return super.lerpFrom(a, t);
  }

  @override
  ShapeBorder? lerpTo(ShapeBorder? b, double t) {
    if (b is StarShape)
      return StarShape(side: BorderSide.lerp(side, b.side, t));
    return super.lerpTo(b, t);
  }

  @override
  Path getInnerPath(Rect rect, {TextDirection? textDirection}) {
    Rect rect1 =
        Rect.fromLTRB(rect.left, rect.top, rect.right, rect.bottom - 200);
    print("宽高：${rect.toString()}");
    return Path()
      ..addRRect(RRect.fromRectAndRadius(rect1, Radius.circular(100)));
  }

  @override
  Path getOuterPath(Rect rect, {TextDirection? textDirection}) {
    print("宽高1：${rect.toString()}");
    Rect rect1 =
        Rect.fromLTRB(rect.left - 100, rect.top, rect.right, rect.bottom - 200);
    return Path()
      ..addRRect(RRect.fromRectAndRadius(rect1, Radius.circular(100)));
  }

  @override
  StarShape copyWith({BorderSide? side}) {
    return StarShape(side: side ?? this.side);
  }

  @override
  void paint(Canvas canvas, Rect rect, {TextDirection? textDirection}) {
    switch (side.style) {
      case BorderStyle.none:
        break;
      case BorderStyle.solid:
        {
          print("宽高2：${rect.toString()}");
          canvas.drawRect(rect, side.toPaint());
        }

      // canvas.drawCircle(rect.center, (rect.shortestSide - side.width) / 2.0,
      //     side.toPaint());
    }
  }

  @override
  bool operator ==(Object other) {
    if (other.runtimeType != runtimeType) return false;
    return other is StarShape && other.side == side;
  }

  @override
  int get hashCode => side.hashCode;

  @override
  String toString() {
    return '${objectRuntimeType(this, 'StarShape')}($side)';
  }
}
